package com.xuecheng.search.service;

import com.xuecheng.framework.domain.search.TeachplanMediaSearch;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.search.search.TeachplanMediaSearchRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 查询课程计划节点和视频URL
 */
@Service
public class EsTeachplanMediaService {
    @Resource
    private TeachplanMediaSearchRepository teachplanMediaPubRepository;

    /**
     * 根据课程计划的节点id查询课程计划节点对应的视频地址
     *
     * @param teachplanId 课程计划id
     * @return ES里面记录的课程计划和视频url
     */
    public QueryResponseResult<TeachplanMediaSearch> getTeachplanMediaByTeachplanId(String[] teachplanId) {
        try {
            if (StringUtils.isNotEmpty(Arrays.toString(teachplanId))) {
                List<TeachplanMediaSearch> result = teachplanMediaPubRepository.findByTeachplanidIn(Arrays.asList(teachplanId));
                if (result.size()==0){
                    return new QueryResponseResult<>(CommonCode.FAIL, null);
                }
                QueryResult<TeachplanMediaSearch> queryResult = new QueryResult<>();
                queryResult.setList(result);
                queryResult.setTotal(result.size());
                return new QueryResponseResult<>(CommonCode.SUCCESS, queryResult);
            }else {
                return new QueryResponseResult<>(CommonCode.FAIL, null);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new QueryResponseResult<>(CommonCode.FAIL, null);
        }
    }
}
